package ai.people.netmon.msgsearch.grpcservice;

import ai.people.core.grpc.lib.es.*;
import ai.people.core.grpc.utils.ProtoJsonUtil;
import ai.people.core.grpc.utils.StreamObserverDelegate;
import ai.people.netmon.framework.domain.searchmsg.request.WarningMessageRequest;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.msgsearch.domain.response.InfoDataResult;
import ai.people.netmon.msgsearch.service.WarningMessageService;
import com.alibaba.fastjson.JSON;
import com.google.protobuf.Message;
import io.grpc.stub.StreamObserver;
import lombok.RequiredArgsConstructor;
import net.devh.boot.grpc.server.service.GrpcService;

/**
 * 警告消息grpc服务
 *
 * @author yuanqinglong
 * @date 2022/8/13 16:27
 */
@GrpcService
@RequiredArgsConstructor
public class WarningMessageGrpcService extends WarningMessageServiceGrpc.WarningMessageServiceImplBase {


    private final WarningMessageService warningMessageServiceImpl;

    /**
     * 警告消息
     *
     * @param request          请求
     * @param responseObserver 响应观察者
     */
    @Override
    public void warningMessage(WarningMessageGrpcRequest request, StreamObserver<SearchPageResponse> responseObserver) {
        StreamObserverDelegate.build(responseObserver).execute(() -> {
            WarningMessageRequest warningMessageRequest = ProtoJsonUtil.toJava(request, WarningMessageRequest.class);
            PageResponse<InfoDataResult> esWarningMessageVOPageResponse = warningMessageServiceImpl.warningMessage(warningMessageRequest);
            SearchPageResponse.Builder builder = SearchPageResponse.newBuilder();
            for (InfoDataResult infoDataResult : esWarningMessageVOPageResponse.getList()) {
                Message message = ProtoJsonUtil.toProtoMessage(SearchResponseData.newBuilder(), JSON.toJSONString(infoDataResult));
                builder.addList((SearchResponseData)message);
            }
            builder.setTotal(esWarningMessageVOPageResponse.getTotal());
            return builder.build();
        });
    }
}
